package com.czy.learning.questionnature.controller;

import com.czy.learning.questionnature.dto.QuestionNatureDTO;
import com.czy.learning.questionnature.service.QuestionNatureService;
import com.lin.learning.questionmanagement.service.QuestionService;
import com.lin.learning.questionmanagement.service.UploadService;
import com.our.learning.infrastructure.pojo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author :czy
 * @date : 2023/6/22 20:16
 */
@RestController
@RequestMapping("/questionnature")
public class QuestionNatureController {

    @Autowired
    private  QuestionNatureService questionNatureService;

    private UploadService uploadService;

    private QuestionService questionService;

    @GetMapping("/listPage")
    public ResponseResult getQuestionsNature(Integer page, Integer perPage,Integer id,String knowledge,String chapter){
        return questionNatureService.dividePage(page,perPage,id,knowledge,chapter);
    }
    @GetMapping("/list")
    public ResponseResult questionNatureList(Integer id,String knowledge,String chapter) {
        return questionNatureService.questionNatureList(id,knowledge,chapter);
    }

    @PostMapping
    public ResponseResult addQuestionNature(@RequestBody QuestionNatureDTO questionNatureDTO){
        return questionNatureService.addQuestionNature(questionNatureDTO);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteQuestionNature(@PathVariable Integer id){
        return questionNatureService.deleteQuestionNature(id);
    }

    @PutMapping
    public ResponseResult updateQuestionNature(@RequestBody QuestionNatureDTO questionNatureDTO){
        return questionNatureService.updateQuestionNature(questionNatureDTO);
    }

    @GetMapping("type/{id}")
    public ResponseResult getQuestionById(@PathVariable Integer id){
        return  questionNatureService.getQuestionNature(id);
    }

    @PostMapping("/upload")
    public ResponseResult uploadQuestion(@RequestParam("file") MultipartFile multipartFile){
        try{
            return uploadService.upload(multipartFile);
        }catch (IOException e){
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
    }

    @GetMapping("/download")
    public ResponseResult downloadQuestionOrPdf(String path){
        try{
            return uploadService.downloadOss(path);
        }catch (IOException e){
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
    }

}
